package io.github.uicdb.badskill.clent.impl;

import io.github.uicdb.badskill.clent.TextView;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.Component;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Supplier;

public class VecScTextView extends TextView {

    protected float scrollY;



    public VecScTextView(int x, int y, int w, int h, Supplier<List<? extends Component>> list) {
        super(x, y, w, h,list);
    }


    @Override
    public void render(@NotNull GuiGraphics pPoseStack, int pMouseX, int pMouseY, float pPartialTick) {
        pPoseStack.enableScissor(getX(),getY(),getX()+width,getY()+height);
        renderWidget(pPoseStack, pMouseX, pMouseY, pPartialTick);
        pPoseStack.disableScissor();
    }

    @Override
    protected float getStartY() {
        return getY()-scrollY;
    }

    public float getContentHeight(){
        return Math.max(getTextHeight(), height);
    }

    private float getMaxScroll()
    {
        return this.getContentHeight() -this.height ;
    }

    private void applyScrollLimits()
    {
        float max = getMaxScroll();

        if (max < 0)
        {
            max /= 2;
        }

        if (this.scrollY < 0.0F)
        {
            this.scrollY = 0.0F;
        }

        if (this.scrollY > max)
        {
            this.scrollY = max;
        }
    }
    @Override
    public boolean mouseScrolled(double pMouseX, double pMouseY, double pDelta) {
        if(isMouseOver(pMouseX,pMouseY)){
                scrollY = (int) (scrollY-pDelta*getScrollAmount());
                applyScrollLimits();
            return true;
        }
        return false;
    }
    protected int getScrollAmount()
    {
        return 20;
    }
}
